define(['jquery'], function($) {

	function BuildTable(opt) {
		var defaultOpt = {
			tableWrap: document.getElementById('main-table-wrap'),  // 表格最外层div
			tableCont: document.getElementById('main-table-content'), // 展示区域的主表
			fixedTbody: [], // 左右两侧固定列表的tbody区域
			footerH: 0, // 底部列表导航区域， (table-div已经设置了padding-bottom: 74)
			scrollBarW: 17, // 滚动条宽度， chrome是17, 其他浏览器可能不太一样， 不过也差不多
			theadH: 44,  // 表头的高度（table有border）
			fixedLeftNum: 1, // 左侧固定列数量
			fixedRightNum: 1, // 右侧固定列数量
		};

		// 传递的参数和默认配置合并
		for (var k in opt) {
			if (defaultOpt[k] !== opt[k]) {
				defaultOpt[k] = opt[k]
			}
		}

		this.opt = defaultOpt;

		var tableWid = $('.main-table-content').width(),
			tbodyWid = $('.main-table-tbody table').width();
		if ( tableWid == (tbodyWid + 1) || tableWid == tbodyWid) {
			this.opt.scrollBarW = 0;
		}
		if ($('.fixed-column').length) {
			// table宽度不设为100%自适应
			$('.main-table-content table, .main-table-content .fixed-table, .main-table-content .fixed-table-content table').css('width', '150px');
		}

		// 列表最大高度
		$('.main-table-content').css('max-height', $(window).height() - 300);

		 if (this.isIE()) {
		 	$('.main-table-content table thead tr th').css('line-height', '44px');
		 	$('.main-table-content .main-table-tbody').css('padding-right', '17px');
		 }

		this.mainTableTbody = this.opt.tableCont.querySelector('.main-table-tbody'); // 中间的表头
		this.mainTableThead = this.opt.tableCont.querySelector('.main-table-thead'); // 中间的tbody
		this.fixedLeft = null; // 左侧固定列的div
		this.fixedRight = null; // 右侧固定列的div
		
		// thead + tbody的高度主表的高度， 用于与最外层div的高度做比较。 最外层div的高度是继承tableWrap的高度的
		var h = this.mainTableThead.offsetHeight + this.mainTableTbody.offsetHeight; 
		// 设置wraHeigt的值小于等于tableWrap的高度
		this.wrapHeight = this.opt.tableCont.offsetHeight - this.opt.footerH > h ? h : this.opt.tableCont.offsetHeight - this.opt.footerH;
		this.wrapWidth = this.opt.tableCont.offsetWidth; 
		this.mainTableThead.style.width = this.wrapWidth - this.opt.scrollBarW + 'px'; // 设置宽度为去除右侧滚动条的宽度
		this.mainTableTbody.style.height = this.wrapHeight - this.opt.theadH + 'px'; // 设置高度为去除底部滚动条的高度
		
	 	if ($('.fixed-column').length) {
	 		this.setFixedW();
		 	this.createFixedTable(); // 生成左右两侧固定的列表
		 	this.bindScrollEvent(); // 绑定滚动事件
	 	}
	}

	/**
	 * 判断是否IE
	 */
	BuildTable.prototype.isIE = function() {
		if (!!window.ActiveXObject || "ActiveXObject" in window) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 设置左侧和右侧固定列宽
	 */
	BuildTable.prototype.setFixedW = function() {
		var ths = this.mainTableThead.querySelectorAll('th');
			len = ths.length,
			leftW = 0,
			rightW = 0;

		for (var i = 0; i < this.opt.fixedLeftNum && i < len; i++) {
			leftW += ths[i].offsetWidth;
		}

		for (var n = this.opt.fixedRightNum; n > 0; n--) {
			var m = this.opt.fixedRightNum - n;
			rightW += ths[len-1-m].offsetWidth;
		}
	 
		this.opt.fixedLeftW = leftW; // 用于设置左侧固定表格父div的宽度
		this.opt.fixedRightW = rightW; // 用于设置右侧固定表格父div的宽度
	}

	/**
	 * [生成左右两侧固定的列表]
	 * @return {[type]} [description]
	 */
	BuildTable.prototype.createFixedTable = function() {
		var head = this.mainTableThead.innerHTML,
			cont = this.mainTableTbody.innerHTML;

		var fixedLeft = document.createElement('div'),  // 左侧固定div
			fixedLeftH = document.createElement('div'), // 左侧固定thead的div
			fixedLeftB = document.createElement('div'), // 左侧固定tbody的div
			fixedRight = document.createElement('div'), // 右侧固定div
			fixedRightH = document.createElement('div'), // 右侧固定thead的div
			fixedRightB = document.createElement('div'); // 右侧固定tbody的div

		fixedLeft.className = 'fixed-table fixed-left';
		fixedLeftH.className = 'fixed-table-content fixed-thead';
		fixedLeftH.innerHTML = head;
		fixedLeftB.className = 'fixed-table-content fixed-tbody';
		fixedLeftB.innerHTML = cont;

		fixedRight.className = 'fixed-table fixed-right';
		fixedRightH.className = 'fixed-table-content fixed-thead';
		fixedRightH.innerHTML = head;
		fixedRightB.className = 'fixed-table-content fixed-tbody';
		fixedRightB.innerHTML = cont;

		fixedLeft.appendChild(fixedLeftH);
		fixedLeft.appendChild(fixedLeftB);

		fixedRight.appendChild(fixedRightH);
		fixedRight.appendChild(fixedRightB);

		this.opt.fixedTbody.push(fixedLeftB);
		this.opt.fixedTbody.push(fixedRightB);
	 
		fixedLeftB.style.height = this.wrapHeight - this.opt.theadH - this.opt.scrollBarW + 'px';
		fixedRightB.style.height = this.wrapHeight - this.opt.theadH - this.opt.scrollBarW + 'px';

		fixedLeft.style.height = this.wrapHeight - this.opt.scrollBarW + 'px';
		fixedRight.style.height = this.wrapHeight - this.opt.scrollBarW + 'px';
	 

		// 设置左侧固定列的宽高 
		fixedLeft.style.width = this.opt.fixedLeftW + 'px';

		// 设置右侧固定列的宽高 
		fixedRight.style.width = this.opt.fixedRightW + 'px';

		this.opt.tableCont.appendChild(fixedLeft);
		this.opt.tableCont.appendChild(fixedRight);
		this.fixedLeft = fixedLeft;
		this.fixedRight = fixedRight;
		this.showShadow();
		this.removeInputName();
	 
		if (this.mainTableTbody.scrollHeight <= this.mainTableTbody.offsetHeight) {
			this.fixedRight.style.right = 0;
			if (this.isIE()) {
				this.fixedRight.style.right = '17px';
			}
		}

		// $('.fixed-table-content').width($('.main-table-wrap').width());

	}

	/**
	 * 绑定滚动事件， 滚动的时候固定列一起跟着滚动
	 * @return {[type]} [description]
	 */
	BuildTable.prototype.bindScrollEvent = function() {
		var _this = this;

		$(_this.mainTableTbody).on('scroll', function() {
			var left = this.scrollLeft,
				top  = this.scrollTop,
				tbody = _this.opt.fixedTbody; 

			_this.mainTableThead.scrollLeft = left;  // 若要生效 需先设置overflow属性
			
			for(var i = 0, len = tbody.length; i < len; i++) { 
				tbody[i].scrollTop = top;
			}

			_this.showShadow();
		});
	} 

	/**
	 * 移除左右固定列表的input的name的值
	 * @return {[type]} [description]
	 */
	BuildTable.prototype.removeInputName = function() {
		var leftInput = this.fixedLeft.querySelectorAll('input'),
			rightInput = this.fixedRight.querySelectorAll('input');

		for (var i = 0, len = leftInput.lenght; i < len; i++) {
			leftInput[i].name = '';
		}

		for (var n = 0, len = leftInput.lenght; n < len; n++) {
			rightInput[n].name = '';
		}
	}

	/**
	 * 显示固定列的box-shadow
	 * @return {[type]} [description]
	 */
	BuildTable.prototype.showShadow = function() {
		var mainTableTbody = this.mainTableTbody;  
			barW = this.opt.scrollBarW;

		if (this.mainTableTbody.scrollHeight <= this.mainTableTbody.offsetHeight) {
			barW = 0;
		}

		if (mainTableTbody.scrollLeft > 0) {
			if (this.fixedLeft.className.indexOf('box-shadow') < 0) {
				this.fixedLeft.className += ' box-shadow'; 
			}
		} else {
			this.fixedLeft.className = 'fixed-table fixed-left';
		}

		if (mainTableTbody.scrollWidth - mainTableTbody.scrollLeft > mainTableTbody.offsetWidth - barW) {
			if (this.fixedRight.className.indexOf('box-shadow') < 0) {
				this.fixedRight.className += ' box-shadow'; 
			} 
		} else {
			this.fixedRight.className = 'fixed-table fixed-right';
		}
	}
	return BuildTable;
});
 
